package com.ziyia.a3_5_1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private MyHandler myHandler = new MyHandler(new WeakReference<>(this));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);
    }

    public void clickHandle(View view) {

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {

                myHandler.sendEmptyMessage(100);
            }
        }, 0,  1200);
    }

    class MyHandler extends Handler {
        private WeakReference<MainActivity> activity;

        public MyHandler(WeakReference<MainActivity> activity) {
            this.activity = activity;
        }
        @Override
        public void handleMessage(@NonNull Message msg) {

            if (msg.what == 100) {
                activity.get().textView.setText("" + System.currentTimeMillis());
            }
        }
    }
}